<template>
  <el-form class="add-event-rank-friend" v-if="wbData">
    <el-divider content-position="center">好友数据</el-divider>
    <input-normal label="排行关联id" v-model="wbData.relateCompId" @change="onUpdateWbComponent"></input-normal>
    <dropdown-normal v-model="wbData.rankType" :valLists="FRIEND_RANK_TYPES" label="好友接口类型" :bIsBtn="false" @change="onUpdateWbComponent" ></dropdown-normal>
    <el-button type="primary"  class="margin-left-right-16 margin-bottom-10" @click.stop="onAddWbItem"><el-icon><i class="jy-icon-plus"></i></el-icon>添加数据项</el-button>
    <p class="margin-left-right-16 red">下面的数据项只是为了布局UI，在移动端预览的时候会填充真实的数据</p>
    <el-collapse v-model="activeNames">
      <el-collapse-item v-for="(item, index) in eventWbLists" :key="'drag_' + index" :name="index+1" class="list-group-item">
        <template #title>
          好友{{index+1}}<i class="jy-icon-delete el-collapse-item__arrow" @click.stop="onDeleteWbItem($event, index)"></i>
        </template>
        <input-normal label="uid" v-model="item.uid" @change="onUpdateWbItem(index)"></input-normal>
      </el-collapse-item>
    </el-collapse>
  </el-form>
</template>

<script setup lang="ts">
import { useWebControlsStore } from '@/store/web/controls'
import { useWebComponentStore } from '@/store/web/component'
import { getWbListByItem } from '@/components/utils'
import { WbComponentDatas } from '@/const/web/'
import { FRIEND_RANK_TYPES } from '@/const/'

const props = defineProps<{ 
  event: IEvent,
  index: number,
}>()

const event = props.event
const useWebControls = useWebControlsStore()
const useWebComponent = useWebComponentStore()

const  activeNames = [1]
let wbData_: any
const wbData = computed(() => {
  try {
    wbData_ = useWebControls.currentControls![event.controlId] && useWebControls.currentControls![event.controlId].data
  } catch (error) {
    console.log("controlData.data null")
  }
  return wbData_
})

let wbLists_: any[]
const eventWbLists = computed(() => {
  if(wbData_.elements) {
    wbLists_ = wbData_.elements
  }
  return wbLists_
})
const onUpdateWbComponent = () => {
  useWebControls.updateWbComponent({ controlId: event.controlId, data: wbData_ })
}
const onAddWbItem = (e: any) => {
  e.stopPropagation()
  e.preventDefault()
  const controlId = event.controlId
  let item: any
  const wbList = getWbListByItem(useWebComponent.currentComponentData!)
  if(wbList[0]) {
    item = _.cloneDeep(wbList[0])
    item.mid = ''
  } else {
    item = _.cloneDeep(WbComponentDatas[event.controlId].data.elements[0])
  }
  useWebControls.addWbComponentItem({controlId, item})
}
const onDeleteWbItem = (e: any, index: number) => {
  e.stopPropagation()
  e.preventDefault()
  const params = {controlId: event.controlId, index}
  useWebControls.deleteWbComponentItem(params)
}
const onUpdateWbItem = (index: number) => {
  const controlId = event.controlId
  const wbList = getWbListByItem(useWebComponent.currentComponentData!)
  const params = {item: wbList[index], index, controlId}
  useWebControls.updateWbComponentItem(params)
}
</script>